package com.luyuan.crm.question.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.hibernate.criterion.Order;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.Member;
import com.luyuan.crm.question.model.Question;
import com.luyuan.crm.question.service.QuestionValueService;
import com.luyuan.crm.question.service.QuestionService;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;

@Component("questionListAction")
@Scope("view")
public class QuestionListAction extends BaseAction {

    private Member member;

    @Resource(name = "questionService")
    private QuestionService questionService;

    @Resource(name = "questionValueService")
    private QuestionValueService questionValueService;

    public DataModel getTableModel() {
        TablePage tablePage = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<Order> orders = new ArrayList<Order>();
        for (TableOrder tableOrder : tableOrders) {
            Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }

        CriteriaTemplate c = CriteriaTemplate.forClass(Question.class);

        Page page = questionService.pagedQuery(c, tablePage.getPage(), tablePage.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public void delQuestion(int index) {
        questionService.remove(index);
        questionValueService.removeByQuestionId(index);
    }

    public Member getMember() {
        if (this.member == null) {
            member = new Member();
        }
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

}
